#ifndef OSDEAMONSERVICE_H
#define OSDEAMONSERVICE_H

#include <QObject>
#include <QJsonObject>
#include <QThread>
#include <QProcess>
#include <QTimer>
#include "MqttClient/mqttdevicereg.h"
#include "MqttClient/mqttdaemon.h"
#include "daemonproxy.h"
#include "configmanage.h"
#include "downloadtasks.h"

Q_DECLARE_METATYPE(QProcess::ExitStatus)
class OSDeamonService : public QObject
{
    Q_OBJECT
public:
    enum class PckRunMode{
        PRM_Direct,         //下载完成直接运行
        PRM_Condition       //未下载，需要下载再运行
    };
    Q_ENUM(PckRunMode)
    typedef struct {
        QString pckPath;
        QString md5Code;        
    }PckPathCheckCode;
    explicit OSDeamonService(QObject *parent = nullptr);
    ~OSDeamonService();
    
    void start();
    
    QList<QJsonObject> m_tasksQueue;
    QList<PckPathCheckCode> m_fileSavePath;
    int m_queueSize;
    bool dlExistError() const;
    void setDlExistError(bool newDlExistError);
    quint64 configTimerout();
private:
    MqttDeviceReg* m_mqttDevReg = nullptr;
    MqttDaemon* m_mqttDaemon = nullptr;
    DaemonProxy* m_daemonProxy = nullptr;
    ConfigManage* m_configManage = nullptr;
    DownloadTasks* m_downloadTasks = nullptr;
    
    QThread* m_thd = nullptr;
    QTimer* m_timerHeart = nullptr;
    
    bool m_registerOk = false;
    bool m_connectMqttDaemon = false;
    bool m_dlExistError = false;
    bool m_upgradeSilent = false;                               //false:提醒升级，true:强制升级
    bool m_isPckDateOK = false;
    
    bool m_isBindAgent = false;
    
    bool m_updateFail = false;
    bool m_updataVersionDate = false;
    bool m_requestFirmware = false;
    QStringList m_popup={"user","auto"};
private slots:
    void initMqttDaemon(QMap<QString,QString> mqttInfoMap);
    void onMqttRegister();
    void onRecvDeamonConn();
    void onRegisterRespons(QJsonObject);
    void onRecvDownloadFirmware(QJsonObject);
    void onPostOtaFirmwareGet();
    void onUpdateLogic();
    void installUpdatePackage(QList<PckPathCheckCode> packagePath);
    void httpReply(const QString& http,QString result,int code);
    
    bool checkNetwork();
    bool md5Checkout(QList<PckPathCheckCode>);
    bool unzipPackage(QList<QString>);
    bool runUpdateShell(QList<QString>);
    bool isUpdatePckExists(const QList<QJsonObject>&,QList<PckPathCheckCode>&);
    void delDownloadFile();
    //void mqttDaemonPublish();
    void sendMacIP();
public slots:
    void onInit();
    void onDownloadFinished(QObject* obj = nullptr);
    void onFinished();
    void onReadTaskFile();
    void onDoUpgrade();
    void onShutdown();
    void onReboot();
    void onSDDMRegister();
    void onPower();
signals:
    void sigStartDownloadTasks();
    void sigUpdate();
    void sigSocketSendMsg(DaemonProxy::ProcessObj,QString);
    void sigInstallPackage(QList<PckPathCheckCode> packagePath);
    void sigGetNewVersion();

};
Q_DECLARE_METATYPE(OSDeamonService::PckPathCheckCode)
//qRegisterMetaType<OSDeamonService::PckPathCheckCode>("OSDeamonService::PckPathCheckCode");
#endif // OSDEAMONSERVICE_H
/*
 * 这个类，主要是用来处理产品的业务逻辑事项的;
 * 
*/
